﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using ApiBetaSeries.Model;
using ApiBetaSeries.Utils;

namespace ApiBetaSeries.Factories
{
    public class SeasonFactory
    {
        /// <summary>
        /// Create a season from an XmlNode
        /// </summary>
        /// <param name="urlShow">url of the show</param>
        /// <param name="e">Xml node</param>
        /// <returns>a season</returns>
        [ApiBetaSeries.Version.ApiMethod(true, "shows/episodes")]
        public static ISeason CreateSeason(string urlShow, XmlNode e)
        {
            ISeason season = new Season();
            season.Number = UtilsXML.GetIntValue(e, "number");
            season.UrlShow = urlShow;
            season.Episodes = new List<IEpisode>();

            if (e["episodes"] == null)
            {
                season.Episodes = null;
            }
            else
            {

                foreach (XmlElement eps in e["episodes"].GetElementsByTagName("episode"))
                {
                    // there is an episode inside episode...
                    if (eps.ParentNode.Name.Equals("episode"))
                        continue;
                    IEpisode episode = EpisodeFactory.CreateEpisode(eps);
                    episode.UrlShow = season.UrlShow;
                    episode.Season = season.Number;
                    season.Episodes.Add(episode);
                }
            }

            return season;
        }

        /// <summary>
        /// Create a season
        /// </summary>
        /// <returns>a season</returns>
        public static ISeason CreateSeason()
        {
            return new Season();
        }
    }
}
